package com.xfly.app.ui;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.utils.AllUtils;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.adapter.TeamAdapter;
import com.xfly.app.bean.TeamBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class YjActivity extends BaseMvpActivity {

    TitleBarView titleBarView;

    TextView text1, text2;

    View view1, view2;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    TeamAdapter mTeamAdapter;
    PopupWindow popupWindow;
    private View popView;
    String type = "1";
    Calendar calendar = Calendar.getInstance(Locale.CHINA);
    ListView listview;
    TextView time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_team);
    }

    @Override
    public void initViews() {
        titleBarView = findViewById(R.id.title_bar);
        text1 = findViewById(R.id.text1);
        text2 = findViewById(R.id.text2);
        view1 = findViewById(R.id.view1);
        view2 = findViewById(R.id.view2);
        listview = findViewById(R.id.listview);
        time = findViewById(R.id.time);

        titleBarView.setText("我的团队");
    }

    @Override
    public void initEvents() {
        titleBarView.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text1.setTextColor(0xFFE6212A);
                text2.setTextColor(0xff666666);
                view1.setVisibility(View.VISIBLE);
                view2.setVisibility(View.INVISIBLE);
                type = "1";
                getlist("","");

                time.setText("全部");
            }
        });

        text2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                text2.setTextColor(0xFFE6212A);
                text1.setTextColor(0xff666666);
                view2.setVisibility(View.VISIBLE);
                view1.setVisibility(View.INVISIBLE);

                type = "2";

                getlist("","");
                time.setText("全部");

            }
        });

        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop();
            }
        });
        getlist("","");

    }


    private void pop() {
        if (null == popupWindow) {
            if (null == popView) {
                popView = LayoutInflater.from(this).inflate(R.layout.pop_teamtime, null);

                popView.findViewById(R.id.endtime).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                            // 绑定监听器(How the parent is notified that the date is set.)
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                // 此处得到选择的时间，可以进行你想要的操作
//                        tv.setText("您选择了：" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                                ((TextView)popView.findViewById(R.id.endtime)).setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth );

                            }
                        }
                                // 设置初始日期
                                , calendar.get(Calendar.YEAR)
                                , calendar.get(Calendar.MONTH)
                                , calendar.get(Calendar.DAY_OF_MONTH)).show();

                    }
                });
                popView.findViewById(R.id.starttime).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                            // 绑定监听器(How the parent is notified that the date is set.)
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                // 此处得到选择的时间，可以进行你想要的操作
//                        tv.setText("您选择了：" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                                ((TextView)popView.findViewById(R.id.starttime)).setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth );

                            }
                        }
                                // 设置初始日期
                                , calendar.get(Calendar.YEAR)
                                , calendar.get(Calendar.MONTH)
                                , calendar.get(Calendar.DAY_OF_MONTH)).show();

                    }
                });

                popView.findViewById(R.id.pay).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            if( TextUtils.isEmpty(((TextView)popView.findViewById(R.id.starttime)).getText().toString())
                        ||TextUtils.isEmpty(((TextView)popView.findViewById(R.id.endtime)).getText().toString())){
                                showMsg("请选择时间");
                                return;
                            }
                            popupWindow.dismiss();
                            time.setText(((TextView)popView.findViewById(R.id.starttime)).getText().toString()+" - "
                            +((TextView)popView.findViewById(R.id.endtime)).getText().toString());
                            getlist(dateToStamp(((TextView)popView.findViewById(R.id.starttime)).getText().toString())
                                    ,dateToStamp(((TextView)popView.findViewById(R.id.endtime)).getText().toString()));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                });

                popView.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popupWindow.dismiss();
                    }
                });
            }
//            View view = LayoutInflater.from(this).inflate(R.layout.personaldata_pop, null);
            popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
            popupWindow.setAnimationStyle(R.style.dialogAnim);
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
            AllUtils.backgroundAlpha(0.5f, getWindow());
            popupWindow.showAtLocation(popView, Gravity.BOTTOM, 0, 0);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    AllUtils.backgroundAlpha(1.0f, getWindow());
                }
            });
            //popupWindow.showAtLocation(rootView, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        } else {
            AllUtils.backgroundAlpha(0.5f, getWindow());
            popupWindow.showAtLocation(popView, Gravity.BOTTOM, 0, 0);
        }

    }

    private void getlist(String stime,String etime) {
        Map<String, String> map = new HashMap<>();
        map.put("group", type);
        map.put("stime", stime);
        map.put("etime", etime);
        apiDataRepository.getTeamList(map, new ApiNetResponse<List<TeamBean>>() {
            @Override
            public void onSuccess(List<TeamBean> data) {
                mTeamAdapter = new TeamAdapter(getContext(),data,type);
                listview.setAdapter(mTeamAdapter);
            }
        });
    }


    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException{
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

}
